﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTab;

namespace LanuageInfo.Forms.Controls
{
    #region 带有多标签功能的工作区控件
    /// <summary>
    /// 带有多标签功能的工作区控件
    /// </summary>
    public partial class UcWorkArea : UserControl
    {
        public UcWorkArea()
        {
            InitializeComponent();
            if (!this.DesignMode)
            {
                tcMain.TabPages.Clear();
            }
        }
        private string pageId;

        public string TabId
        {
            get { return pageId; }
        }

        /// <summary>
        /// 添加窗体
        /// </summary>
        /// <param name="name">标签名称</param>
        /// <param name="frm">窗体对象</param>
        /// <param name="image">图标</param>
        /// <param name="onlyOne">是否唯一name标签</param>
        public void AddForm(string name, WAFormBase frm, Image image = null, bool onlyOne = true)
        {
            AddForm(name, name, frm, image, onlyOne);
        }

        /// <summary>
        /// 添加窗体
        /// </summary>
        /// <param name="tabID">标签ID</param>
        /// <param name="name">标签名称</param>
        /// <param name="frm">窗体对象</param>
        /// <param name="image">图标</param>
        /// <param name="onlyOne">是否唯一tabID名称标签</param>
        public void AddForm(string tabID, string name, WAFormBase frm, Image image = null, bool onlyOne = true)
        {
            pageId = tabID;
            if (onlyOne)
            {
                foreach (XtraTabPage page in tcMain.TabPages)
                {
                    if (page.Name == tabID)
                    {
                        tcMain.SelectedTabPage = page;
                        //frm.Close();
                        frm.Dispose();
                        frm = null;
                        return;
                    }
                }
            }

            XtraTabPage tp = new XtraTabPage();
            tp.Name = tabID;
            tp.Text = name;
            tp.Image = image;
            //frm.FormBorderStyle = FormBorderStyle.None;
            //frm.TopLevel = false;
            frm.Text = name;
            frm.Visible = true;
            frm.Dock = DockStyle.Fill;
            tcMain.TabPages.Add(tp);
            tcMain.SelectedTabPage = tp;
            tp.Controls.Add(frm);
            frm.Show();
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="name"></param>
        public void CloseForm(string name)
        {
            XtraTabPage closePage = null;
            foreach (XtraTabPage page in tcMain.TabPages)
            {
                if (page.Name == name)
                {
                    closePage = page;
                }
            }
            if (closePage != null)
            {
                tcMain.TabPages.Remove(closePage);
            }
        }

        /// <summary>
        /// 是否指定的标签名称的标签已存在
        /// </summary>
        /// <param name="tabName">标签名称</param>
        /// <returns></returns>
        public bool IsTabExist(string tabName)
        {
            foreach (XtraTabPage page in tcMain.TabPages)
            {
                if (page.Name == tabName)
                {
                    return true;
                }
            }
            return false;
        }



        /// <summary>
        /// 激活标签
        /// </summary>
        /// <param name="tabName">标签名称</param>
        public void ActiveTab(string tabName)
        {
            foreach (XtraTabPage page in tcMain.TabPages)
            {
                if (page.Name == tabName)
                {
                    tcMain.SelectedTabPage = page;
                    return;
                }
            }
        }

        public WAFormBase ActiveWAForm
        {
            get
            {
                if (tcMain.SelectedTabPage != null)
                {
                    return tcMain.SelectedTabPage.Controls[0] as WAFormBase;
                }
                return null;
            }
        }

        public WAFormBase RetWAForm(string tabName)
        {
            foreach (XtraTabPage page in tcMain.TabPages)
            {
                if (page.Name == tabName)
                {
                    WAFormBase objBase = page.Controls[0] as WAFormBase;
                    return objBase;
                }
            }
            return null;
        }

        /// <summary>
        /// 是否存在未保存数据
        /// </summary>
        /// <returns></returns>
        public bool IsDirty()
        {
            foreach (XtraTabPage page in tcMain.TabPages)
            {
                WAFormBase frm = (WAFormBase)page.Controls[0];
                if (frm.IsDirty)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 关闭按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tcMain_CloseButtonClick(object sender, EventArgs e)
        {
            WAFormBase frm = (WAFormBase)tcMain.SelectedTabPage.Controls[0];
            if (frm.IsDirty && frm.OnIsClose(true))
            {
                DialogResult dr = MessageBox.Show("当前正要关闭的窗体存在未保存的数据，是否仍要关闭它？", "确认关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr != DialogResult.OK)
                {
                    return;
                }
            }
            int selectedIndex = tcMain.SelectedTabPageIndex;
            tcMain.TabPages.Remove(tcMain.SelectedTabPage);
            if (selectedIndex > 0)
            {
                tcMain.SelectedTabPageIndex = selectedIndex - 1;
            }
            //frm.Close();
            frm.Dispose();
            frm = null;
            GC.Collect();
        }
        /// <summary>
        /// 选择页面改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tcMain_SelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            foreach (XtraTabPage page in tcMain.TabPages)
            {
                if (page.Controls.Count == 0)
                {
                    continue;
                }
                WAFormBase frm = (WAFormBase)page.Controls[0];
                if (tcMain.SelectedTabPage == page)
                {
                    frm.OnActiveChange(true);
                }
                else
                {
                    frm.OnActiveChange(false);
                }
            }
        }
    }
    #endregion

    #region 工作区窗体基类
    /// <summary>
    /// 工作区窗体基类
    /// </summary>
    public class WAFormBase : UserControl
    {
        /// <summary>
        /// 窗体的接受按钮
        /// </summary>
        [Description("窗体的接受按钮")]
        public IButtonControl AcceptButton { get; set; }

        /// <summary>
        /// 后退的窗体
        /// </summary>
        public WAFormBase BackwardForm { get; set; }

        /// <summary>
        /// 前进的窗体
        /// </summary>
        public WAFormBase ForewardForm { get; set; }

        /// <summary>
        /// 是否有未保存数据
        /// </summary>
        public virtual bool IsDirty
        {
            get
            {
                return false;
            }
            set
            {
                //不做处理
            }
        }

        

       

        /// <summary>
        /// 跳转到新窗体
        /// </summary>
        /// <param name="name">标签名称</param>
        /// <param name="frm">需要跳转到的窗体对象</param>
        public void GoToForm(string name, WAFormBase frm, bool bindForm = true)
        {
            Control parent = this.Parent;
            parent.Controls.Remove(this);
            //frm.FormBorderStyle = FormBorderStyle.None;
            //frm.TopLevel = false;
            frm.Visible = true;
            frm.Dock = DockStyle.Fill;
            frm.Text = name;
            if (bindForm)
            {
                frm.BackwardForm = this;
                this.ForewardForm = frm;
            }
            //parent.Name = name;
            parent.Text = frm.Text;
            parent.Controls.Add(frm);
            frm.Show();
        }

        /// <summary>
        /// 后退到上一窗体
        /// </summary>
        public void Backward()
        {
            if (this.BackwardForm != null)
            {
                GoToForm(this.BackwardForm.Text, this.BackwardForm, false);
            }
        }

        /// <summary>
        /// 前进到上一窗体
        /// </summary>
        public void Foreward()
        {
            if (this.ForewardForm != null)
            {
                GoToForm(this.ForewardForm.Text, this.ForewardForm, false);
            }
        }

        #region 加载数据

        /// <summary>
        /// 异步加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BeginLoadData()
        {
            this.BeginInvoke(new EventHandler(LoadData), this, EventArgs.Empty);
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void LoadData()
        {
            return;
        }

        private void LoadData(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region 激活状态改变
        public virtual void OnActiveChange(bool isActive)
        {
            return;
        }
        #endregion

        #region 关闭时是否保存数据
        public virtual bool OnIsClose(bool isSave)
        {
            return isSave;
        }
        #endregion


        #region 按键消息处理
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                if (this.AcceptButton != null)
                {
                    this.AcceptButton.PerformClick();
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        #endregion
    }
    #endregion
}
